package com.alex.monitor.starter.demo.controller;

import cn.hutool.core.io.IoUtil;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 模拟http 报警请求接受
 *
 * @author Alex.Liu
 * @date: 2025-02-17 13:00
 * @since 1.0.0
 */
@Slf4j
@RestController
public class EchoController {


    /**
     * @param request
     * @return
     */
    @GetMapping("/echo")
    public String echoGet(HttpServletRequest request) {
        try {
            String warn = IoUtil.readUtf8(request.getInputStream());
            log.info("URI={} WARN={}", request.getRequestURI(), warn);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "echo:" + System.currentTimeMillis();
    }

    /**
     * @param request
     * @return
     */
    @PostMapping("/echo")
    public String echoPost(HttpServletRequest request) {
        try {
            String warn = IoUtil.readUtf8(request.getInputStream());
            log.info("URI={} WARN={}", request.getRequestURI(), warn);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "echo:" + System.currentTimeMillis();
    }

}